home *** CD-ROM | disk | FTP | other *** search
- Path: news.mindlink.net!news
- From: genew@mindlink.bc.ca (Gene Wirchenko)
- Newsgroups: comp.lang.c
- Subject: Re: Mem Allocation in functions
- Date: Sat, 13 Jan 1996 06:34:24 GMT
- Organization: MIND LINK! - British Columbia, Canada
- Message-ID: <4d7js3$hql@fountain.mindlink.net>
- References: <00001a80+00006ba7@msn.com>
- NNTP-Posting-Host: line205.nwm.mindlink.net
- X-Newsreader: Forte Free Agent 1.0.82
-
- Gordon_B@msn.com (Gordon Thomas) wrote:
-
- >I should know the answer, but I don't. I have a function
-
- >#include <usual stuff>
- > void foo(float *stuff, int *length)
- > {
- > int i;
- > stuff = calloc(*length, sizeof(float));
- >// diddle around and put numbers in stuff[i]
- > return;
- > }
- >//
- > int main(void)
- > {
- > int count;
- > float *somenums;
- > int np = 20;
- > void foo(float *, int *);
- > foo(somenums,&np);
- > for(count = 0 ; count < np ; count++)
- > fprintf(stdout,"%4d %10.2f\n",count, somenums[count] ;
- > free(somenums);
- > return 0;
- > }
- >My question is: in foo, memory was allocated to the pointer "somenums" and
- >at the return the array was filled with the correct values. However,
- >in main the array is junk NANs, and in fact the pointer is pointing into DS.
- >On the other hand, if "somenums" is calloc'd in main, correct values are
- >obtained.
- >What language spec am I ignorant of that vitiates the above scheme.
- >Thanx
- >Gordon
-
- C uses call by value. In order to modify a pointer, you must
- pass a pointer to it i.e. pointer to pointer to whatever.
-
- Sincerely,
-
- Gene Wirchenko
-
- C Pronunciation Guide:
- y=x++; "wye equals ex plus plus semicolon"
- x=x++; "ex equals ex doublecross semicolon"
-
-